﻿<?php
function sentct_cookie($c_name) {
  $c_value=@date("Y-m-d");
  $timelimit=@time()+365*24*3600*1000;
  if (@strlen($cname.$c_value)<=4000)
  {
    echo "document.cookie = \"".$c_name."=".$c_value."; expires=".$timelimit."\";";
  }
  else
  {
    echo "if (comfirm(\"Cookie exceeds 4kB and will be cut!\"))";
    echo "{document.cookie = \"".$c_name."=".$c_value."; expires=".$timelimit."\";}";
  }
}
function strexists($haystack, $needle) {
  return !(strpos($haystack, $needle) === FALSE);
}
function robot_detect() {
  if(!defined('IS_ROBOT'))
  {
    $kw_spiders = 'Bot|Crawl|Spider|slurp|sohu-search|lycos|robozilla';
    $kw_browsers = 'MSIE|Netscape|Opera|Konqueror|Mozilla';
    if(!strexists($_SERVER['HTTP_USER_AGENT'], 'http://') && preg_match("/($kw_browsers)/i", $_SERVER['HTTP_USER_AGENT'])) {
      define('IS_ROBOT', FALSE);
    } 
    elseif(preg_match("/($kw_spiders)/i", $_SERVER['HTTP_USER_AGENT'])) {
      define('IS_ROBOT', TRUE);
    } 
    else {
      define('IS_ROBOT', FALSE);
    }
  }
  return IS_ROBOT;
}

$dpath="pvrec/";
$filename=$dpath."p".(int)$_GET['pid'].".pct";
$vday=@date("Y-m-d");
$isRobot=robot_detect();
if($isRobot==FALSE)
{
  sentct_cookie('counter');
  if(!file_exists($filename))
  {
    $fp=@fopen($filename,"w");
    @fwrite($fp,"1");
    @fclose($fp);
    echo "document.write(\"本文已被瀏覽過1次\");";
  }
  else if($_COOKIE["counter"]==$vday)
  {
    $ct=@file($filename);
    echo "document.write(\"本文已被瀏覽過".$ct[0]."次\");";
  }
  else
  { 
    sentct_cookie('counter');
    $ct=@file($filename);
    $ct[0]++;
    $fp=@fopen($filename,"w");
    @fwrite($fp,$ct[0]);
    @fclose($fp);
    echo "document.write(\"本文已被瀏覽過".$ct[0]."次\");";
  }
}
else
{
  exit;
}
?>